/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xuidemo.slice.tabbar.provider;

import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.PageSliderProvider;
import ohos.agp.components.StackLayout;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.TextAlignment;


import java.util.List;

/**
 * EasyindicatorProvider
 *
 * @since 2021-04-16
 */
public class EasyIndicatorProvider extends PageSliderProvider {
    private static final int TEXT_SIZE = 50;
    private static final int TEXT_COLOR = 0xff9FABBC;
    // 数据源，每个页面对应list中的一项
    private List<DataItem> list;

    /**
     * EasyIndicatorProvider
     *
     * @param list
     */
    public EasyIndicatorProvider(List<DataItem> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object createPageInContainer(ComponentContainer componentContainer, int i) {
        final DataItem data = list.get(i);
        Text label = new Text(null);
        label.setTextAlignment(TextAlignment.CENTER);
        label.setLayoutConfig(
            new StackLayout.LayoutConfig(
                ComponentContainer.LayoutConfig.MATCH_PARENT,
                ComponentContainer.LayoutConfig.MATCH_PARENT
            ));
        label.setText(data.mText);
        label.setTextColor(new Color(TEXT_COLOR));
        label.setTextSize(TEXT_SIZE);
        ShapeElement element = new ShapeElement();
        label.setBackground(element);
        componentContainer.addComponent(label);
        return label;
    }

    @Override
    public void destroyPageFromContainer(ComponentContainer componentContainer, int i, Object o) {
        componentContainer.removeComponent((Component) o);
    }

    @Override
    public boolean isPageMatchToObject(Component component, Object o) {
        return true;
    }

    // 数据实体类

    /**
     * DataItem
     *
     * @since 2021-04-16
     */
    public static class DataItem {
        String mText;

        /**
         * DataItem
         *
         * @param txt
         */
        public DataItem(String txt) {
            mText = txt;
        }
    }
}
